﻿using Superstore.Vender.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin;
using Superstore.Vender.App_Code;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;

namespace Superstore.Vender.Controllers
{
    public class UserInfoController : Controller
    {
        protected static readonly string AppId = WebConfigurationManager.AppSettings["WeixinAppId"];//与微信公众账号后台的AppId设置保持一致，区分大小写。
        protected static readonly string AppSecret = WebConfigurationManager.AppSettings["WeixinAppSecret"];//与微信公众账号后台的AppSecret设置保持一致，区
        protected static readonly string Domain = WebConfigurationManager.AppSettings["WeixinDomain"];//微信服务器主机域名

        private readonly static string USERINFO = "userInfo";
        protected void setOpenId(string openId)
        {
            if (!string.IsNullOrEmpty(openId) && !hasOpenId())
            {
                UserInfo userInfo = new UserInfo(openId);
                Session[USERINFO] = userInfo;
            }
        }
        protected Boolean hasOpenId()
        {
            UserInfo userInfo = (UserInfo)Session[USERINFO];
            return (null != userInfo) && (!string.IsNullOrEmpty(userInfo.openId));
        }
        protected string getOpenId()
        {
            string openId = null;
            UserInfo userInfo = (UserInfo)Session[USERINFO];
            if (null != userInfo)
            {
                openId = userInfo.openId;
            }
            return openId;
        }
        protected ActionResult check(string code, string state, string url)
        {
            //ViewBag.OpenId = "oqjDVt6Mnv1Ig1mCW2YXmAbVVsss";
            //setOpenId(ViewBag.OpenId);
            //WXInfoCacheMgr.SetLatitude(22.871747, ViewBag.OpenId);
            //WXInfoCacheMgr.SetLongitude(113.208838, ViewBag.OpenId);
            ////WXInfoCacheMgr.SetLatitude(22, ViewBag.OpenId);
            ////WXInfoCacheMgr.SetLongitude(113, ViewBag.OpenId);
            //return View(); 

            if (hasOpenId())
            {
                ViewBag.OpenId = getOpenId();
                return View();
            }
            else
            {
                if (!string.IsNullOrEmpty(code) && state == AuthController.AuthorizeState)
                {
                    //通过，用code换取access_token
                    var result = OAuthApi.GetAccessToken(AppId, AppSecret, code);
                    if (result.errcode == ReturnCode.请求成功)
                    {
                        setOpenId(result.openid);
                        ViewBag.OpenId = getOpenId();
                    }
                    return View();
                }
                else
                {
                    string returnUrl = string.Format("{0}{1}", Domain, url);
                    return RedirectToAction("Index", "Auth", new { returnUrl = returnUrl });
                }
            }
        }
    }
}